<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

/**
 * SectionsController
 * 
 * @package JL Panel
 * @author JLQ
 * @copyright 2009
 * @version 1.0
 * @access public
 */
class LiensAdminController extends JLControllerBase {

    private $config = null;
    private $path = "Liens";
    private $title = "Système d'Actualisation CSME > Liens";
    private $data = null;
    private $dirAdhesion = "documents";

    /**
     * LiensController::getLiens()
     * 
     * @return
     */
    function getLien() {
        $id = $_POST['_id'];

        require JLPATH_MODELS . DS . "LiensAdminModel.php";
        $liens = new LiensAdminModel();
        $lien = $liens->getLiensById($id);

        $var_lien_title = $lien['var_lien_title'];
        $var_lin_link = $lien['var_lin_link'];
        $var_file_single = $lien['var_file_single'];
        $int_id_state = $lien['int_id_state'];

        echo $var_lien_title . "_%_" . $var_lin_link . "_%_" . $var_file_single . "_%_" . $int_id_state;
    }

    /**
     * LiensController::updateLiens()
     * 
     * @return
     */
    function updateLien() {
        $id = $_POST['_id'];
        $var_lien_title = $_POST['_var_lien_title'];
        $var_lin_link = $_POST['_var_lin_link'];
        $var_file_single = $_POST['_var_file_single'];
        $var_file_img = $this->getFileTypeIconImgTag($var_lin_link);
        $int_id_administration = $_POST['_int_id_administration'];
        $int_id_state = $_POST['_int_id_state'];

        require JLPATH_MODELS . DS . "LiensAdminModel.php";
        $liens = new LiensAdminModel();
        $jl = new JLTools();
        $var_extension = str_replace('docx', 'doc', $jl->getFileExtension($var_file_single));
        $var_extension = str_replace('xlsx', 'xls', $var_extension);

        $lien = $liens->getLiensById($id);
        if ($lien['var_file_single'] != $var_file_single)
            @unlink(JLPATH_WEB_SITE . DS . $this->dirAdhesion . DS . $lien['var_file_single']);

        $_true = $liens->updateLien($id, $var_lien_title, $var_lin_link, $var_file_single, $var_file_img, $var_extension, $int_id_administration, $int_id_state);

        if ($_true)
            echo "Données sauvegarder avec succès.";
        else
            echo "";
    }

    /**
     * LiensController::insertLiens()
     * 
     * @return
     */
    function insertLien() {
        $var_lien_title = $_POST['_var_lien_title'];
        $var_lin_link = $_POST['_var_lin_link'];
        $var_file_single = $_POST['_var_file_single'];
        $var_file_img = $this->getFileTypeIconImgTag($var_lin_link);
        $int_id_administration = $_POST['_int_id_administration'];
        $int_id_state = $_POST['_int_id_state'];

        require JLPATH_MODELS . DS . "LiensAdminModel.php";
        $liens = new LiensAdminModel();
        $jl = new JLTools();
        $var_extension = str_replace('docx', 'doc', $jl->getFileExtension($var_file_single));
        $var_extension = str_replace('xlsx', 'xls', $var_extension);

        $_true = $liens->insertLien($var_lien_title, $var_lin_link, $var_file_single, $var_file_img, $var_extension, $int_id_administration, $int_id_state);

        if ($_true)
            echo "Données sauvegarder avec succès.";
        else
            echo "";
    }

    function getFileTypeIconImgTag($path) {
        $jl = new JLTools();

        switch ($jl->getFileExtension($path)) {
            case "jpg":
                $img = "pictures.png";
                $order = '4';
                break;

            case "pdf":
                $img = "pdf.png";
                $order = '1';
                break;

            case "doc";
            case "docx";
                $img = "word.png";
                $order = '2';
                break;

            case "xls";
            case "xlsx";
                $img = "excel.png";
                $order = '3';
                break;
        }

        /* if( strtolower($_SERVER['HTTP_HOST']) != strtolower(basename( JLPATH_WEB_SITE )))
          $path = $jl->getNameDomain()."/".basename( JLPATH_WEB_SITE )."/images/24x24/".$img;
          else */
        $path = $jl->getNameDomain() . "/images/24x24/" . $img;

        $img2 = '<!--' . $order . '--><img width="24" height="24" src="' . $path . '" />';
        return $img2;
    }

    /**
     * LiensController::deleteLien()
     * 
     * @return
     */
    function deleteLien() {
        $ids = $_POST['_ids'];
        $ides = explode(",", $ids);
        require JLPATH_MODELS . DS . "LiensAdminModel.php";
        $liens = new LiensAdminModel();

        $i = 0;
        foreach ($ides as $id) {
            $lien = $liens->getLiensById($id);
            @unlink(JLPATH_WEB_SITE . DS . $this->dirAdhesion . DS . $lien['var_file_single']);
            $_true = $liens->deleteLien($id);

            if (!$_true)
                break;

            $i++;
        }

        $nodelete = count($ides) - $i;

        if (!$_true) {
            echo $i . "  Donnée(s) effacer \n$nodelete donnée(s) n'a pu être effacer. Veuillez changer l'état à Provisoire.";
        }
        else
            echo $i . "  Donnée(s) effacer";
    }

    function getFilePath() {
        $path = $_POST['_path'];
        $jl = new JLTools();

        /* if( strtolower($_SERVER['HTTP_HOST']) != strtolower(basename( JLPATH_WEB_SITE )))
          echo $jl->getNameDomain()."/".basename( JLPATH_WEB_SITE )."/".$this->dirAdhesion."/".$path;
          else */
        echo $jl->getNameDomain() . "/" . $this->dirAdhesion . "/" . $path;
    }

}